<?php
defined('IN_MAIN') or exit('Invalid Access!');

function build_admin_search_ui($searching, $submit_to, $checkbox_search, $text_search, $searchfields_ordering, $textbox_style, $extra_hiddenfields = ''){
global $_GET, $get_h;

//build search checkbox, prefix with "_so_"
foreach($_GET as $field=>$value){
 if(preg_match('/^_so_/', $field)){
  preg_match('/(?<=_so_).+(?=__)/', $field, $matchf);
  $f=$matchf[0];
  preg_match('/(?<=__).+$/', $field, $matchv);
  $v=$matchv[0];
  $r_searchfields[$f][]=$v;

 }
}
foreach($checkbox_search as $field => $details){
 $search_checkbox="";
 for($i=0;$i<count($details["option"]);$i++){
  $checked="";
  for($j=0;$j<count($r_searchfields[$field]);$j++){
   if($details["option"][$i]==$r_searchfields[$field][$j]){
    $checked="checked='checked'";
    $j=count($r_searchfields[$field]);
   }
  }
  $search_checkbox.="
  <input type='checkbox' name='_so_".$field."__".$details["option"][$i]."' $checked id='searchf_".$field."_$i' /> <label for='searchf_".$field."_$i'>".$details["option_display"][$details["option"][$i]]."</label><br />\n";
 }
 $checkbox_search_field[]=$field;
 $checkbox_search_row[$field]['title']=$details['display'];
 $checkbox_search_row[$field]['value']="$search_checkbox<br />\n".__("Leave all unchecked to include all options.")."<hr noshade size='1'/>";
}
if(!is_array($checkbox_search_field)){
 $checkbox_search_field=array();
}

//build search textbox, prefix with "_sp_"
foreach($text_search as $field => $dis){
 $textbox_search_field[]=$field;
 $textbox_search_row[$field]["title"]=$dis;
 $textbox_search_row[$field]["value"]="<input type='text' name='_sp_$field' $textbox_style value=\"".$get_h["_sp_$field"]."\" />";
}
if(!is_array($textbox_search_field)){
 $textbox_search_field=array();
}

//search javascript
if($searching){
 $search_onload="show_search();";
}
$search_jvscript=
"<script type='text/javascript' src='".JS_URL."/common.js'></script>
<script type='text/javascript'>
$search_onload
function show_search(){
 if(q('search_div').style.display=='block'){
  q('search_div').style.display='none';
  q('srq').innerHTML='".AddSlashes(__("Show Search"))." &raquo;';
 }else{
  q('search_div').style.display='block';
  q('srq').innerHTML='&laquo; ".AddSlashes(__("Hide Search"))."';
 }
}
</script>";

//search fields ordering & generation
foreach($searchfields_ordering as $searchfield){
 if(is_array($searchfield)){//custom search field
  $title=$searchfield['title'];
  $value=$searchfield['value'];
 }elseif(in_array($searchfield, $textbox_search_field)){
  $title=$textbox_search_row[$searchfield]['title'];
  $value=$textbox_search_row[$searchfield]['value'];
 }elseif(in_array($searchfield, $checkbox_search_field)){
  $title=$checkbox_search_row[$searchfield]['title'];
  $value=$checkbox_search_row[$searchfield]['value'];
 }
 $searchfields.="
 <tr>
  <td width='125'>$title:</td>
  <td>$value</td>
 </tr>";
}

//search ui
$search_ui="
<a href=\"javascript:show_search();\"><span id='srq'>".__("Show Search")." &raquo;</span></a>
<div id='search_div' style=\"display:none;\">
<form name='search_form' method='get' action='$submit_to'>
<input type='hidden' name='s' value='1' />
$extra_hiddenfields
<table class='ams_table'>
 $searchfields
 <tr>
  <td colspan='2' class='center'><input type='submit' value='".__("Search")."' /></td>
 </tr>
</table>
</form>
</div>
$search_jvscript";

return $search_ui;
}
?>